<?php

class UsersController extends RestController
{
    public function indexAction()
    {
//    	$uid = $this->getRequest()->getParam("uid");
//		$pass = $this->getRequest()->getParam("pass");
		$sql = "SELECT * FROM competency.user";
		$result = $this->getDB()->fetchAll($sql);
		//print_r($result);		
		//$this->getResponse()->appendBody($result);
    	//echo $uid;
//    	$this->getResponse()
//    	->setHttpResponseCode(200)
//    	->setHeader('Content-Type', 'text/html;charstet=UTF-8;')
//    	->appendBody("Response:". $uid);
    }

    public function getAction()
    {
    	$uid = $this->_getParam('id');
		//	$q = new 
		$result = UserQuery::create()->findPk($uid);	
		if ($result == null)
		{
			//$this->getResponse()->setHttpResponseCode(500);
			throw  new Zend_Db_Exception("Your result is null");			
		}
		else
		{			
			echo "Education object is:";
			print_r ($result->getEducations());
			//print_r($result->toArray());
			echo Zend_Json::encode($result->toArray());
		}		    	
    }
    
    public function postAction()
    {
    	$uid = $this->_getParam('uid');	
    	$request = $this->getRequest();
    	if($uid <> null && $request->isPost())
    	{
    		$user= new User();
    		$user->fromArray($this->getRequest()->getUserParams());
//			$email= $this->_getParam('email');
//			$nickname= $this->_getParam('nickname');
//			$firstname = $this->_getParam('firstname');
//			$lastname= $this->_getParam('lastname');
//			$password = $this->_getParam('password');
//			$brithday = $this->_getParam('birthday');
//			$currentstatus = $this->_getParam('currentstatus');
//			
//			$user->setEmail($email);
//			$user->setNickname($nickname);
//			$user->setFirstname($firstname);
//			$user->setLastname($lastname);
//			$user->setPassword($password);
//			$user->setCurrentstatus($currentstatus);
			if ($user->validate())
			{
				$user->save();
			}
			else
			{
				$this->getResponse()->setHttpResponseCode(500);  
				  // Something went wrong.
				  // Use the validationFailures to check what
				  foreach ($user->getValidationFailures() as $failure) {
				    echo $failure->getMessage();
				  }
			}
    	}
    	else
    	{
    		$this->getResponse()->setHttpResponseCode(400);    		
    	}
    }
      /**
     * The put action handles PUT requests and receives an 'id' parameter; it
     * should update the server resource state of the resource identified by
     * the 'id' value.
     */
    public function putAction()
    { 
    	$uid = $this->_getParam('uid');    	
		$request = $this->getRequest();
    	if(!empty($uid) && $request->isPut())
    	{
    		$user = UserQuery::create()->findOneByUid(int($uid));
    		$user->fromArray($this->getRequest()->getUserParams());
//    		$email= $this->_getParam('email');
//			$nickname= $this->_getParam('nickname');
//			$firstname = $this->_getParam('firstname');
//			$lastname= $this->_getParam('lastname');
//			$password = $this->_getParam('password');
//			$brithday = $this->_getParam('birthday');
//			$currentstatus = $this->_getParam('currentstatus');
//			
//			$user->setEmail($email);
//			$user->setNickname($nickname);
//			$user->setFirstname($firstname);
//			$user->setLastname($lastname);
//			$user->setPassword($password);
//			$user->setCurrentstatus($currentstatus);
    		if ($user->validate())
			{
				$user->save();
			}
			else
			{
				$this->getResponse()->setHttpResponseCode(500);  
				  // Something went wrong.
				  // Use the validationFailures to check what
				  foreach ($user->getValidationFailures() as $failure) {
				    echo $failure->getMessage();
				  }
			}
    	}
    }
    
	public function deleteAction()
	{
		$uid = $this->_getParam('id');
		$request = $this->getRequest();
		if(!empty($uid) && $request->isDelete())
    	{
    		$user = UserQuery::create()->findOneByUid(int($uid));
    		$user->delete();
    	}
	}
	/**
	 * Return Array of parameter
	 * 
	 *@var Array 
	 */
	private function fetchParameters()
	{	
		$data = array();
		$uid = $this->_getParam('uid');
		if(!empty($uid))
    	{
    		$data['uid']= $uid;
    	}
    	$email = $this->_getParam('email');
    	if(!empty($email))
    	{
    		$data['email']=$email;
    	}
    	$firstname = $this->_getParam('firstname');
    	if(!empty($firstname))
    	{
    		$data['firstname']=$firstname;
    	}
    	$lastname =$this->_getParam('lastname');
    	if(!empty($lastname))
    	{
    		$data['lastname']=$lastname;
    	}    	
    	$nickname =$this->_getParam('nickname');
    	if(!empty($nickname))
    	{
    		$data['nickname']=$nickname;
    	}    	
    	$password =$this->_getParam('password');
    	if(!empty($password))
    	{
    		$data['password']=$password;
    	}
    	return $data;
	}
}

